package eyrolles;

import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.action.ActionMessage;

public class EditEmployeAction extends ActionDB {

    protected void updateUser(ActionForm form)
            throws SQLException {

        String user = null;

        try {

            EmployeForm eForm = (EmployeForm) form;
            conn = getConnection();
            stmt = conn.createStatement();

            StringBuffer sqlString =
                    new StringBuffer("update employes ");

            sqlString.append("set password='"
                    + eForm.getPassword() + "', ");
            sqlString.append("roleid="
                    + eForm.getRoleid() + ", ");
            sqlString.append("name='"
                    + eForm.getName() + "', ");
            sqlString.append("phone='"
                    + eForm.getPhone() + "', ");
            sqlString.append("email='"
                    + eForm.getEmail() + "', ");
            sqlString.append("depid="
                    + eForm.getDepid());
            sqlString.append(" where username='"
                    + eForm.getUsername() + "'");
            stmt.execute(sqlString.toString());
        }catch (NullPointerException e) {

            throw new SQLException();
        } finally {

            closeConnection();
        }
    }

    public ActionForward execute(ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException {

        // Cible par defaut en cas de succes
        String target = new String("success");

        ActionMessages errors = new ActionMessages();


        // Teste sur l'identification de l'utilisateur
        HttpSession session = request.getSession();
        if (session.getAttribute("USER") == null) {

            // L'utilisateur n'est pas identifie
            target = new String("login");

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.login.required"));


            if (!errors.isEmpty()) {

                saveErrors(request, errors);
            }

            // Si jamais l'utilisateur n'est pas loggé, alors
            // on retourne directement à la page de login
            // avant d'effectuer la moindre requête
            return (mapping.findForward(target));
        }

        if (isCancelled(request)) {

            // Action annulee
            return (mapping.findForward(target));
        }

        try {

            updateUser(form);
        } catch (SQLException e) {

            target = new String("error");

            errors.add(ActionMessages.GLOBAL_MESSAGE,
                    new ActionMessage("errors.database.error"));

        }

        // Signaler les erreurs eventuelles
        if (!errors.isEmpty()) {

            saveErrors(request, errors);
        }
        
        // Transmettre la requete a la vue appropriee
        return (mapping.findForward(target));
    }
}